package com.younion.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.common.base.BaseDAO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.*;

 
public class CustomerDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(CustomerDAO.class);

	 
	public Collection selectAll() throws BaseException {
		return queryForList("t_Customer.selectAll", null);
	}

	 
	public Collection selectByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return null;
		try {
			return queryForList("t_Customer.selectByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public int selectCountByMap(Map paramMap) throws BaseException {
		try {
			return queryCount("t_Customer.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_Customer.selectAll";
			} else {
				sqlStr = "t_Customer.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CustomerVO insertByVo(CustomerVO originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			Integer id = (Integer) this.getSqlMapClientTemplate().insert("t_Customer.insertByVo", originalVo);
			originalVo.setId(id);
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CustomerVO[] insertVos(CustomerVO[] originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			for (int i = 0; i < originalVo.length; i++)
				originalVo[i] = insertByVo(originalVo[i]);
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public Collection insertByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				CustomerVO data = (CustomerVO) it.next();
				data = insertByVo(data);
			}
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return false;
		try {
			this.update("t_Customer.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateVos(Map[] originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			for (int i = 0; i < originalVo.length; i++) {
				if (!updateByMap(originalVo[i]))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean updateByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				Map data = (Map) it.next();
				if (!updateByMap(data))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteAll() throws BaseException {
		try {
			return this.getSqlMapClientTemplate().delete(
					"t_Customer.deleteAll", null) > 0 ? true : false;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public CustomerVO selectById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return null;
		try {
			return (CustomerVO) this.getSqlMapClientTemplate().queryForObject(
					"t_Customer.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Customer.deleteById",
					paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByIds(List list) throws BaseException {
		if (CollectionUtils.isEmpty(list))
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Customer.deleteByIds",
					list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean deleteByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate()
					.delete("t_Customer.deleteByMap", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public boolean relieveByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return false;
		try {
			this.update("t_Customer.relieveByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	public boolean recharge(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate()
					.update("t_Customer.recharge", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}


	public List selectByIds(List ids) throws BaseException {
		return queryForList("t_Customer.selectByIds", ids);
	}
	
	public void updateTopById(Map map) throws BaseException {
		try {
			this.getSqlMapClientTemplate()
					.update("t_Customer.updateTopById", map);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	public int selectMaxTop() throws BaseException {
		try {
			Object obj = this.getSqlMapClientTemplate().queryForObject(
					"t_Customer.selectMaxTop");
			if (obj == null)
				return 0;
			return ((Integer) obj).intValue();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}
	
	 
	public boolean deleteByIdsRelated(List<String> list) throws BaseException {
		if (list == null || list.isEmpty())
			return false;
		try {
			for(String customerId : list){
				Map<String,Integer> map = new HashMap<String,Integer>();
				map.put("id", Integer.valueOf(customerId));
				//delete active
				this.getSqlMapClientTemplate().delete("t_active.deleteByCustomerId", map);
				//delete advertising and pic,must delete before deleting user
				this.getSqlMapClientTemplate().delete("t_advertising.deleteByCustomerId", map);
				//delete affiche
				this.getSqlMapClientTemplate().delete("t_affiche.deleteByCustomerId", map);
				//delete card agio
				this.getSqlMapClientTemplate().delete("T_Card_Agio.deleteByCustomerId", map);
				//delete card count init
				this.getSqlMapClientTemplate().delete("t_card_count_init.deleteByCustomerId",map);
				//delete card level
				this.getSqlMapClientTemplate().delete("t_Card_Level.deleteByCustomerId",map);
				//delete cost
				this.getSqlMapClientTemplate().delete("t_Cost.deleteByCustomerId",map);
				{
				//delete favorites ,must delete before favourable entity and card entity
				this.getSqlMapClientTemplate().delete("T_Favorites.deleteFavByCustomerId",map);
				this.getSqlMapClientTemplate().delete("T_Favorites.deleteCardByCustomerId",map);
				}
				//delete favourable ,must be deleted before favourable entity
				this.getSqlMapClientTemplate().delete("t_Favourable.deleteByCustomerId",map);
				//delete favourable entity
				this.getSqlMapClientTemplate().delete("t_Favourable_Entity.deleteByCustomerId",map);
				//delect favourable type
				this.getSqlMapClientTemplate().delete("t_Favourable_Type.deleteByCustomerId", map);
				//delete member card cost count ,must delete before member card 
				this.getSqlMapClientTemplate().delete("T_Member_Card_CostCount.deleteByCustomerId", map);
				//delete member card service , must delete before member card 
				this.getSqlMapClientTemplate().delete("T_Membercard_Service.deleteByCustomerId", map);
				//delete member card upgrade log
				this.getSqlMapClientTemplate().delete("t_member_card_upgrade_log.deleteByCustomerId", map);
				//delete member card , must delete before card entity
				this.getSqlMapClientTemplate().delete("t_Member_Card.deleteByCustomerId", map);
				//delete service init , must delete before card entity
				this.getSqlMapClientTemplate().delete("t_Service_Init.deleteByCustomerId",map);
				//delete recharge rule , must delete before card entity
				this.getSqlMapClientTemplate().delete("t_Recharge_Rule.deleteByCustomerId",map);
				//delete upgrade rule, must delete before card entity
				this.getSqlMapClientTemplate().delete("t_Upgrade_Rule.deleteByCustomerId", map);
				//delete card entity
				this.getSqlMapClientTemplate().delete("t_Card_Entity.deleteByCustomerId",map);
				//delete message
				this.getSqlMapClientTemplate().delete("T_Message.deleteByCustomerId", map);
				//delete notificationTaskLog 
				this.getSqlMapClientTemplate().delete("T_NotificationTask_Log.deleteByCustomerId", map);
				//delete operate log 
				this.getSqlMapClientTemplate().delete("t_operate_log.deleteByCustomerId", map);
				//delete recharge feed back task log
				this.getSqlMapClientTemplate().delete("T_RechargeFeedback_TaskLog.deleteByCustomerId", map);
				//delete recharge log
				this.getSqlMapClientTemplate().delete("t_RechargeLog.deleteByCustomerId", map);
				//delete sendMsg 
				this.getSqlMapClientTemplate().delete("T_SendMsg.deleteByCustomerId", map);
				//delete product ,must delete before product kind
				this.getSqlMapClientTemplate().delete("t_Product.deleteByCustomerId", map);
				//delete product kind
				this.getSqlMapClientTemplate().delete("t_Product_Kind.deleteByCustomerId", map);
				//delete user ,must delete before roles
				this.getSqlMapClientTemplate().delete("t_User.deleteByCustomerId", map);
				//delete roles
				this.getSqlMapClientTemplate().delete("t_Roles.deleteByCustomerId", map);
				//delete shop
				this.getSqlMapClientTemplate().delete("t_Shop.deleteByCustomerId", map);
				//delete customer
				this.getSqlMapClientTemplate().delete("t_Customer.deleteById",map);
			
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" CustomerDAO，调用deleteByIdsRelated(List<String> list)方法出错");
		}
	}

}